/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.to.aboomy.bannersample.indicator.magicindicator;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.StackLayout;
import ohos.app.Context;

/**
 * 整个框架的入口
 *
 * @since 2020-09-29
 */
public class MagicIndicator extends StackLayout {
    private IpagerNavigator mNavigator;

    /**
     * 构造
     *
     * @param context context
     */
    public MagicIndicator(Context context) {
        super(context);
    }

    /**
     * 构造方法
     *
     * @param context context
     * @param attrs attrs
     */
    public MagicIndicator(Context context, AttrSet attrs) {
        super(context, attrs);
    }

    /**
     * 滑动监听
     *
     * @param position position
     * @param positionOffset positionOffset
     * @param positionOffsetPixels positionOffsetPixels
     */
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if (mNavigator != null) {
            mNavigator.onPageScrolled(position, positionOffset, positionOffsetPixels);
        }
    }

    /**
     * 选中监听
     *
     * @param position
     */
    public void onPageSelected(int position) {
        if (mNavigator != null) {
            mNavigator.onPageSelected(position);
        }
    }

    /**
     * z状态监听
     *
     * @param state 状态
     */
    public void onPageScrollStateChanged(int state) {
        if (mNavigator != null) {
            mNavigator.onPageScrollStateChanged(state);
        }
    }

    /**
     * getNavigator
     *
     * @return IpagerNavigator
     */
    public IpagerNavigator getNavigator() {
        return mNavigator;
    }

    /**
     * setNavigator
     *
     * @param navigator navigator
     */
    public void setNavigator(IpagerNavigator navigator) {
        if (mNavigator == navigator) {
            return;
        }
        if (mNavigator != null) {
            mNavigator.onDetachFromMagicIndicator();
        }
        mNavigator = navigator;
        removeAllComponents();
        if (mNavigator instanceof Component) {
            LayoutConfig lp = new LayoutConfig(LayoutConfig.MATCH_PARENT, LayoutConfig.MATCH_PARENT);
            addComponent((Component) mNavigator, lp);
            mNavigator.onAttachToMagicIndicator();
        }
    }
}
